<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Angerbird extends BaseModel
{
    protected $connection = 'api';
    protected $abConfigTable = 'angerbird_config';
    protected $abEventTable = 'angerbird_event';
    protected $abStatisticsTable = 'angerbird_statistics';
    protected $abShopTable = 'angerbird_shop';
    protected $userTable = 'user';
    protected $room_table = 'room';//房间表
    protected $union_room = 'union_room';//公会房间
    protected $union = 'union';//公会表
    protected $unionUserTable = 'union_user';//公会用户
    protected $giftTable = 'gift_library';//礼物表

    //愤怒的小鸟游戏配置获取
    public function angerbirdGetConfig(Request $request)
    {
        $list = DB::connection($this->connection)->table($this->abConfigTable)->get();
        $list = objToArray($list);

        return ['code'=>20000, 'data'=>$list, 'msg'=>'ok'];
    }

    //愤怒的小鸟配置修改
    public function angerbirdUpdateConfig($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/angerbirdUpdateConfig', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //愤怒的小鸟兑换商店列表
    public function angerbirdGetShopList(Request $request)
    {
        $if_show = $request->input('if_show', 0);//是否显示 1=显示 2=隐藏

        $query = DB::connection($this->connection)->table($this->abShopTable.' as a')
            ->leftJoin($this->giftTable.' as b' ,'a.gift_id','=','b.gift_id')
            ->select(['a.id','a.gift_id','a.energy_coin','a.if_show','a.created_at','a.updated_at','b.name as gift_name','b.image as gift_image','b.price as gift_price']);
        if(!empty($if_show)) {
            $query = $query->where('a.if_show', $if_show);
        }

        $list = $query->get();
        $list = objToArray($list);
        if(!empty($list)) {
            foreach($list as $key=>$val) {
                $val['gift_image'] = build_image($val['gift_image']);

                $list[$key] = $val;
            }
        }

        return ['code'=>20000, 'data'=>$list, 'msg'=>'ok'];
    }

    //愤怒的小鸟新增商店
    public function angerbirdAddShop($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/angerbirdAddShop', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

    //愤怒的小鸟修改商店
    public function angerbirdUpdateShop($data, $user){
        $data['admin_id'] = $user['id'];
        $res = curl_request(getKkaudioApiUrl() . '/inner/angerbirdUpdateShop', $data, 'json', 'POST');
        return curlResultHandle($res);
    }

}
